package yxy.game.pm2.command.friend;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FriendMessage;
import yxy.game.pm2.module.chat.HotSpotDataModule;
import yxy.game.pm2.module.chat.SingleChatBalckModule;

import java.util.HashSet;
import java.util.Set;

/**
 * 获取聊天黑名单
 *
 * @ClassName CMD_GetBlackList
 * @Author jingtao-zheng
 * @Date 2022/8/15 19:56
 * @Version 1.0
 **/
@MessageType(proto = Command.GetBlackList, classtype = FriendMessage.GetBlackListRequest.class)
public class CMD_GetBlackList extends Command<FriendMessage.GetBlackListRequest> {

    @Override
    public void execute(MessageSource messageSource, FriendMessage.GetBlackListRequest request) throws Exception {
        //玩家code
        String playerCode = messageSource.getTag();
        FriendMessage.GetBlackListResponse.Builder response = FriendMessage.GetBlackListResponse.newBuilder();
        try {
            //获取全局黑名单
            Set<String> allChatBlack = HotSpotDataModule.getHotSpotData();
            SingleChatBalckModule singleChatBalckModule = SingleChatBalckModule.get(playerCode);
            Set<String> singlechatSet = singleChatBalckModule.getBlackSet();
            Set<String> set = new HashSet<>();
            if (allChatBlack != null) {
                set.addAll(allChatBlack);
            }
            if (singlechatSet != null) {
                set.addAll(singlechatSet);
            }
            //获取个人黑名单
            response.addAllPlayerCodes(set);
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, FriendMessage.GetBlackListResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
